<template>
  <el-form
    v-if="flag"
    ref="ruleFormRef"
    :model="LoserRuleForm"
    :rules="LoserRules"
    label-width="120px"
    class="form"
  >
    <div class="loser">
      <el-form-item label="失踪人姓名" prop="l_name">
        <el-input
          v-model="LoserRuleForm.l_name"
          placeholder="请输入失踪人姓名"
        />
      </el-form-item>

      <el-form-item label="性别" prop="l_sex">
        <el-radio-group v-model="LoserRuleForm.l_sex">
          <el-radio label="男" />
          <el-radio label="女" />
        </el-radio-group>
      </el-form-item>

      <el-form-item label="出生日期" prop="l_birth_date">
        <el-date-picker
          v-model="LoserRuleForm.l_birth_date"
          type="date"
          label="出生日期"
          placeholder="请选择出生日期"
        />
      </el-form-item>

      <el-form-item label="出生地址" prop="l_birth_addr">
        <el-input
          v-model="LoserRuleForm.l_birth_addr"
          placeholder="请输入出生地址"
        />
      </el-form-item>

      <el-form-item label="失踪日期" prop="l_miss_date">
        <el-date-picker
          v-model="LoserRuleForm.l_miss_date"
          type="date"
          label="失踪日期"
          placeholder="请选择失踪日期"
        />
      </el-form-item>

      <el-form-item label="失踪地址" prop="l_miss_addr">
        <el-input
          v-model="LoserRuleForm.l_miss_addr"
          placeholder="请输入失踪地址"
        />
      </el-form-item>

      <el-form-item label="失踪类型" prop="l_type">
        <el-input v-model="LoserRuleForm.l_type" placeholder="请输入失踪类型" />
      </el-form-item>

      <el-form-item label="特征描述" prop="l_descript">
        <el-input v-model="LoserRuleForm.l_descript" type="textarea" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="nextTip(ruleFormRef)"
          >下一步</el-button
        >
      </el-form-item>
    </div>
  </el-form>

  <el-form
    v-else
    ref="LinkFormRef"
    :model="LinkForm"
    :rules="LinkRules"
    label-width="120px"
  >
    <el-form-item label="联系人姓名" prop="m_name">
      <el-input v-model="LinkForm.m_name" placeholder="请输入联系人姓名" />
    </el-form-item>

    <el-form-item label="被寻人与联系人关系" prop="m_relat_l">
      <el-input
        v-model="LinkForm.m_relat_l"
        placeholder="请输入被寻人与联系人的关系"
      />
    </el-form-item>

    <el-form-item label="联系人手机" prop="m_phone">
      <el-input v-model="LinkForm.m_phone" placeholder="请输入联系人手机" />
    </el-form-item>

    <el-form-item label="联系人邮箱" prop="m_email">
      <el-input v-model="LinkForm.m_email" placeholder="请输入联系人邮箱" />
    </el-form-item>

    <el-form-item label="联系人微信" prop="m_wechat">
      <el-input v-model="LinkForm.m_wechat" placeholder="请选择联系人微信" />
    </el-form-item>

    <el-form-item label="联系人地址" prop="m_addr">
      <el-input
        v-model="LinkForm.m_addr"
        placeholder="请输入联系人地址"
        type="textarea"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="flag = true">上一步</el-button>
      <el-button type="primary" @click="submitForm(LinkFormRef, LoserUrl)"
        >提交</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { useLoserRegist } from "@/hooks/loginForm/loserRegist";
import { ElMessage, type FormInstance } from "element-plus";
import { getCurrentInstance, ref } from "vue";
const {
  ruleFormRef,
  LoserRuleForm,
  LoserRules,
  LinkFormRef,
  LinkForm,
  LinkRules,
} = useLoserRegist();

const { proxy }: any = getCurrentInstance();

defineProps<{
  LoserUrl: string;
}>();

const emit = defineEmits(["on-link"]);
const flag = ref(true);

//添加Loser
const nextTip = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (!valid) return;
    flag.value = false;
  });
};

const submitForm = async (
  formEl: FormInstance | undefined,
  LoserUrl: string
) => {
  if (LoserUrl === "") return ElMessage.error("请上传失联人照片");
  const form = { ...LoserRuleForm, ...LinkForm, l_photos: LoserUrl};
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (!valid) return;
    proxy.$http.post("/loser/add", form).then(async (res: any) => {
      ElMessage.success(res);
      emit("on-link", LinkFormRef);
    });
  });
};
</script>

<style scoped lang="less"></style>
